package com.example.spring.boot.rabbitmq.listener;

import com.example.spring.boot.rabbitmq.constant.DirectExchangeConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * <p>
 * 直连队列监听器
 * </p>
 *
 * @author weixin
 * @date 2023/3/14 15:20
 */
@Slf4j
@Component
public class DirectQueueListener {

    /**
     * 尽管设置了两个消费者，但是只有一个能够消费成功
     * 多次发送则轮训消费：
     * DirectReceiver消费者收到消息1  : 发送一条测试消息：direct
     * DirectReceiver消费者收到消息2  : 发送一条测试消息：direct
     * DirectReceiver消费者收到消息1  : 发送一条测试消息：direct
     * DirectReceiver消费者收到消息2  : 发送一条测试消息：direct
     *
     * 一个交换机可以绑定多个队列。如果通过路由key可以匹配到多个队列，消费的时候也只能有一个进行消费
     * @param testMessage
     */
    @RabbitHandler
    @RabbitListener(queues = DirectExchangeConstant.DIRECT_QUEUE)
    public void process(String testMessage) {
        log.info("DirectQueueListener.process, 接收到消息：{}, 接收消息时间：{}",
                testMessage,
                LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"))
        );
    }

    @RabbitHandler
    @RabbitListener(queues = DirectExchangeConstant.DIRECT_QUEUE)
    public void process2(String testMessage) {
        log.info("DirectQueueListener.process2, 接收到消息：{}, 接收消息时间：{}",
                testMessage,
                LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"))
        );
    }

    @RabbitHandler
    @RabbitListener(queues = DirectExchangeConstant.DIRECT_QUEUE2)
    public void process3(String testMessage) {
        log.info("DirectQueueListener.process3, 接收到消息：{}, 接收消息时间：{}",
                testMessage,
                LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"))
        );
    }

}
